package com.gsk.europepharma.salesReport.struts.actions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import net.sf.json.JSONObject;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Query;
import org.hibernate.Session;

import com.gsk.europepharma.salesReport.hibernate.dao.tbSpBoSRSectoDAO;
import com.gsk.europepharma.salesReport.utils.UtilsSalesReport;

/**
 * Gestion des donnees pour le graphe quota.
 */
public class DataQuota extends Action {
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Variables de session
		HttpSession sessionObject = request.getSession();
		Integer cs_adm_pdt = (Integer) sessionObject.getAttribute("cs_adm_pdt");
		Integer cs_adm_rgpt = (Integer) sessionObject.getAttribute("cs_adm_rgpt");
		if (request.getParameter("product") != null) {
			cs_adm_rgpt = Integer.parseInt(request.getParameter("product"));
		}
		Integer cs_adm_prstn = (Integer) sessionObject.getAttribute("cs_adm_prstn");
		Integer cs_network = (Integer) sessionObject.getAttribute("cs_network");
		Integer cs_month = (Integer) sessionObject.getAttribute("cs_month");
		// Fin de session 

		String idGeog = request.getParameter("idGeog");
		Integer idGeogInteger = Integer.parseInt(idGeog);
		
		String source = request.getParameter("source");
		Integer CS_SRC = Integer.parseInt(source);
		String unite = request.getParameter("unite");
		String periode = request.getParameter("periode");
		
		String nivGeog = request.getParameter("nivGeog");
		
		String quota = request.getParameter("quota"); 
		
		String statutProduit = request.getParameter("statutProduit");
		String indicateur = "";
		if (statutProduit.equals("complet")) {
			indicateur = "D_PM";
		} else if (statutProduit.equals("pasDeMarche")) {
			indicateur = "EVOL_VOL";
		} 

		HashMap hm = new HashMap();
		ArrayList categ = new ArrayList();
		ArrayList data1 = new ArrayList();
		ArrayList data2 = new ArrayList();

		tbSpBoSRSectoDAO salesDAO = new tbSpBoSRSectoDAO();
		Session session = salesDAO.getSession();
		
		// Recuperation Mois + PM + Total Marche SERETIDE
		Query req;
		String strReq = "";
		
		if (nivGeog.equals("Secteur") || nivGeog.equals("UGA")) {
			strReq = salesDAO.QUERY_GRAPH_QUOTA_UGA;
		} else {
			strReq = salesDAO.QUERY_GRAPH_QUOTA_SECTO;
		}
		req = session.createSQLQuery(session.getNamedQuery(strReq).getQueryString() + " ORDER BY " + quota + "_" + unite + periode + " DESC" );

		req.setString("data", unite + periode);		
		
		req.setInteger("cs_adm_pdt", cs_adm_pdt);
		req.setInteger("cs_adm_rgpt", cs_adm_rgpt);
		req.setInteger("cs_adm_prstn", cs_adm_prstn);
		req.setInteger("CS_SRC", CS_SRC);
		req.setInteger("cs_network", cs_network);
		req.setInteger("cs_month", cs_month);
		req.setInteger("idGeog", idGeogInteger);
		req.setString("nivGeog", nivGeog);		
		
		String greenColor  = "#00ff00"; 
		String redColor    = "#ff0000"; 
		String yellowColor = "#ffd63a";

		ArrayList result = new ArrayList(req.list());
		Iterator iter = result.iterator();
		while (iter.hasNext()) {
			Object[] val = (Object[]) iter.next();
			
			Double d = 0.0;
			if (!indicateur.equals("")) {
				d = UtilsSalesReport.getIndicateur(indicateur, Double.parseDouble(val[3].toString()), Double.parseDouble(val[4].toString()), Double.parseDouble(val[5].toString()), Double.parseDouble(val[6].toString()), 1);
				data2.add(d);
			}
			//Double d = UtilsSalesReport.getDeltaPm(Double.parseDouble(val[3].toString()), Double.parseDouble(val[4].toString()), Double.parseDouble(val[5].toString()), Double.parseDouble(val[6].toString()), 1);
			categ.add(val[0].toString());
			//data2.add(d);
			 
			Double val1OR2 = 0.0;
			if (quota.equals("QP")) { // quota produit 
				val1OR2 = Double.parseDouble(val[1].toString());
			} else if (quota.equals("QM")) { // quota marche 
				val1OR2 = Double.parseDouble(val[2].toString());
			}
			
			if (d >= 0) {
				data1.add("{ y: " + UtilsSalesReport.arrondir(100 * val1OR2, 1) + ", color: '" + greenColor + "' , idGeog:" + Integer.parseInt(val[7].toString()) + ", niveau:'" + val[8].toString() + "'}");
			} else if (d >= -0.5) {
				data1.add("{ y: " + UtilsSalesReport.arrondir(100 * val1OR2, 1) + ", color: '" + yellowColor + "' , idGeog:" + Integer.parseInt(val[7].toString()) + ", niveau:'" + val[8].toString() + "'}");
			} else {
				data1.add("{ y: " + UtilsSalesReport.arrondir(100 * val1OR2, 1) + ", color: '" + redColor + "' , idGeog:" + Integer.parseInt(val[7].toString()) + ", niveau:'" + val[8].toString() + "'}");
			}
		}

		hm.put("nbData", categ.size());

		hm.put("categ", categ);
		hm.put("data1", data1);
		hm.put("data2", data2);
		
		JSONObject json = JSONObject.fromObject(hm);

		response.addHeader("Content-Type", "application/json");
        response.getOutputStream().write(json.toString().getBytes("UTF-8"));
        response.getOutputStream().flush();

		return null;
	}

}
